package com.tyut.homework.repository;

import com.tyut.homework.pojo.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface StudentRepository extends JpaRepository<Student,Integer> {

    Student findStudentById(Integer id);

    @Query(value = "select s from Student s where s.id like concat('%',?1,'%')")
    List<Student> findByIdLike(Integer id);

    @Query(value = "select s from Student s where s.name like concat('%',?1,'%')")
    List<Student> findByNameLike(String name);

    @Query(value = "select s from Student s where s.phone like concat('%',?1,'%')")
    List<Student> findByPhoneLike(String phone);

    @Transactional
    @Modifying
    @Query(value= "update student set status=1 where id=?1",nativeQuery = true)
    int updateStatus(Integer id);

    @Query(value= "select s from Student s where s.status=0 order by s.clazz asc")
    List<Student> getListNotMatch();

    @Query(value = "select * from student s where s.clazz=?1 limit 0,1",nativeQuery = true)
    Student findInfoByClazz(String clazz);
}
